/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.authorization.domain.SysGrantedAuthority;
import cn.ibizlab.core.authorization.service.SysGrantedAuthorityService;
import cn.ibizlab.core.authorization.filter.SysGrantedAuthoritySearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[SysGrantedAuthority] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractSysGrantedAuthorityResource {

    @Autowired
    public SysGrantedAuthorityService sysGrantedAuthorityService;

    @Autowired
    @Lazy
    public SysGrantedAuthorityDTOMapping sysGrantedAuthorityDtoMapping;

    @Autowired
    @Lazy
    public SysGrantedAuthorityFilterDTOMapping sysGrantedAuthorityFilterDtoMapping;


    /**
    * 创建Create 角色授权资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysGrantedAuthorityDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Create-all') or hasPermission(this.sysGrantedAuthorityDtoMapping.toDomain(#dto),'uaa-SysGrantedAuthority-Create')")
    @PostMapping(value = {"sys_granted_authorities","sys_granted_authorities"})
    public ResponseEntity<SysGrantedAuthorityDTO> create
            (@Validated @RequestBody SysGrantedAuthorityDTO dto) {
        SysGrantedAuthority domain = sysGrantedAuthorityDtoMapping.toDomain(dto);
        sysGrantedAuthorityService.create(domain);
        SysGrantedAuthority rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysGrantedAuthorityDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 角色授权资源
    * 
    *
    * @param id id
    * @return ResponseEntity<SysGrantedAuthorityDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Get-all') or hasPermission(this.sysGrantedAuthorityDtoMapping.toDomain(returnObject.body),'uaa-SysGrantedAuthority-Get')")
    @GetMapping("sys_granted_authorities/{id}")
    public ResponseEntity<SysGrantedAuthorityDTO> getById
            (@PathVariable("id") String id) {
        SysGrantedAuthority rt = sysGrantedAuthorityService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(sysGrantedAuthorityDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 角色授权资源
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Remove-all') or hasPermission(this.sysGrantedAuthorityService.get(#id),'uaa-SysGrantedAuthority-Remove')")
    @DeleteMapping("sys_granted_authorities/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = sysGrantedAuthorityService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 角色授权资源
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<SysGrantedAuthorityDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Update-all') or hasPermission(this.sysGrantedAuthorityService.get(#id),'uaa-SysGrantedAuthority-Update')")
    @VersionCheck(entity = "sysgrantedauthority" , versionfield = "updateTime")
    @PutMapping("sys_granted_authorities/{id}")
    public ResponseEntity<SysGrantedAuthorityDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody SysGrantedAuthorityDTO dto) {
        SysGrantedAuthority domain = sysGrantedAuthorityDtoMapping.toDomain(dto);
        domain.setId(id);
        sysGrantedAuthorityService.update(domain);
        SysGrantedAuthority rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysGrantedAuthorityDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 角色授权资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-CheckKey ")
    @PostMapping("sys_granted_authorities/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody SysGrantedAuthorityDTO dto) {
        SysGrantedAuthority domain = sysGrantedAuthorityDtoMapping.toDomain(dto);
        Integer rt = sysGrantedAuthorityService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 角色授权资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysGrantedAuthorityDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-GetDraft ")
    @GetMapping("sys_granted_authorities/getdraft")
    public ResponseEntity<SysGrantedAuthorityDTO> getDraft
            (@SpringQueryMap SysGrantedAuthorityDTO dto) {
        SysGrantedAuthority domain = sysGrantedAuthorityDtoMapping.toDomain(dto);
        SysGrantedAuthority rt = sysGrantedAuthorityService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(sysGrantedAuthorityDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 角色授权资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<SysGrantedAuthorityDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Save-all') or hasPermission(this.sysGrantedAuthorityDtoMapping.toDomain(#dto),'uaa-SysGrantedAuthority-Save')")
    @PostMapping("sys_granted_authorities/save")
    public ResponseEntity<SysGrantedAuthorityDTO> save
            (@Validated @RequestBody SysGrantedAuthorityDTO dto) {
        SysGrantedAuthority domain = sysGrantedAuthorityDtoMapping.toDomain(dto);
        sysGrantedAuthorityService.save(domain);
        SysGrantedAuthority rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(sysGrantedAuthorityDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchDefault 角色授权资源
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<SysGrantedAuthorityDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"角色授权资源" },  notes = "SysGrantedAuthority-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-FetchDefault-all') or hasPermission(#dto,'uaa-SysGrantedAuthority-FetchDefault')")
    @PostMapping("sys_granted_authorities/fetchdefault")
    public ResponseEntity<List<SysGrantedAuthorityDTO>> fetchDefault
            (@Validated @RequestBody SysGrantedAuthorityFilterDTO dto) {
        SysGrantedAuthoritySearchContext context = sysGrantedAuthorityFilterDtoMapping.toDomain(dto);
        Page<SysGrantedAuthority> domains = sysGrantedAuthorityService.searchDefault(context) ;
        List<SysGrantedAuthorityDTO> list = sysGrantedAuthorityDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建角色授权资源
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Create-all')")
    @ApiOperation(value = "批量新建角色授权资源", tags = {"角色授权资源" },  notes = "批量新建角色授权资源")
	@PostMapping("sys_granted_authorities/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<SysGrantedAuthorityDTO> dtos) {
        sysGrantedAuthorityService.createBatch(sysGrantedAuthorityDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除角色授权资源
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Remove-all')")
    @ApiOperation(value = "批量删除角色授权资源", tags = {"角色授权资源" },  notes = "批量删除角色授权资源")
	@DeleteMapping("sys_granted_authorities/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        sysGrantedAuthorityService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新角色授权资源
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Update-all')")
    @ApiOperation(value = "批量更新角色授权资源", tags = {"角色授权资源" },  notes = "批量更新角色授权资源")
	@PutMapping("sys_granted_authorities/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<SysGrantedAuthorityDTO> dtos) {
        sysGrantedAuthorityService.updateBatch(sysGrantedAuthorityDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存角色授权资源
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Save-all')")
    @ApiOperation(value = "批量保存角色授权资源", tags = {"角色授权资源" },  notes = "批量保存角色授权资源")
	@PostMapping("sys_granted_authorities/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<SysGrantedAuthorityDTO> dtos) {
        sysGrantedAuthorityService.saveBatch(sysGrantedAuthorityDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入角色授权资源
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-SysGrantedAuthority-Save-all')")
    @ApiOperation(value = "批量导入角色授权资源", tags = {"角色授权资源" },  notes = "批量导入角色授权资源")
	@PostMapping("sys_granted_authorities/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<SysGrantedAuthorityDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(sysGrantedAuthorityService.importData(config,ignoreError,sysGrantedAuthorityDtoMapping.toDomain(dtos)));
    }

}
